package com.fhnew.app.ui.activity.recomend.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.ResRecommend;

import com.fhnew.app.util.OnItemClickListener;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/9/25.
 */

public class RecommenddetailsFragment extends Fragment {
    private RecyclerView detailsheader;
    private ImageView refrash;
    private RecyclerView detailscontent;

    private Adapter adapter;
    private Adapter1 adapter1;

    private Boolean flag = true;
    private int onclickpostion;
    private ArrayList<String> otherbean = new ArrayList<>();

    private ArrayList<ResRecommend.DataBean> recombean = new ArrayList<>();
    private ArrayList<ResRecommend.DataBean> recombean1 = new ArrayList<>();//筛选后


    private void initView(View view){
         detailsheader = view.findViewById(R.id.detailsheader);
         refrash = view.findViewById(R.id.refrash);
         detailscontent = view.findViewById(R.id.detailscontent);


    }

    @Nullable
    @Override
    public View onCreateView (@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.recommenddetailslayout,null);
        initView(container);
        return container;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(getContext());
        layoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        detailsheader.setLayoutManager(layoutManager1);
        adapter = new Adapter();
        detailsheader.setAdapter(adapter);


        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        detailscontent.setLayoutManager(layoutManager);
        adapter1 = new Adapter1();
        detailscontent.setAdapter(adapter1);

        refrash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flag = true;
                getdetails();

            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        getdetails();
    }

    private void getdetails() {
        HttpJsonUtils.getHttpApiInstance()
                .getrecommend(Constants.getuseAdress() + "api/m/promotion/recommend", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new CustomObserver<ResRecommend>(RecommenddetailsFragment.this) {
                     .subscribe(new CustomObserver<ResRecommend>() {
                    @Override
                    protected void onCustomNext(ResRecommend resActivity) {
                        if (resActivity.code == 0) {
                            otherbean.clear();
                            otherbean.addAll(resActivity.otherData);
                            adapter.notifyDataSetChanged();
                            recombean.clear();
                            recombean.addAll(resActivity.data);
                            adapter1.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public MViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.addbackitem, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull MViewHolder holder, int position) {
            holder.textitem.setText("" + otherbean.get(position));
            if (onclickpostion == position) {
                holder.myusercodeitem.setBackgroundResource(R.drawable.textbackground7);
            } else {
                holder.myusercodeitem.setBackgroundResource(R.drawable.textbackground);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onclickpostion = position;
                    adapter.notifyDataSetChanged();
                    flag = false;
                    if (recombean != null && recombean.size() > 0) {
                        recombean1.clear();
                        for (int i = 0; i < recombean.size(); i++) {
                            if (Integer.parseInt(recombean.get(i).pid) == position) {
                                recombean1.add(recombean.get(i));
                            }
                        }
                    }
                    adapter1.notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            return otherbean.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            TextView textitem;
            LinearLayout myusercodeitem;

            public MViewHolder(@NonNull View itemView) {
                super(itemView);
                textitem = itemView.findViewById(R.id.myusercode);
                myusercodeitem = itemView.findViewById(R.id.myusercodeitem);
            }
        }
    }

    class Adapter1 extends RecyclerView.Adapter<Adapter1.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public MViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.recommenditem, parent, false));
        }


        @Override
        public void onBindViewHolder(@NonNull MViewHolder holder, int position) {
            if (position % 2 != 0) {
                holder.itemlayout.setBackgroundResource(R.color.blue);
            } else {
                holder.itemlayout.setBackgroundResource(R.color.royalblue);
            }
            if (flag) {
                holder.name.setText(recombean.get(position).name);
                holder.grade.setText(recombean.get(position).level);
                holder.carry.setText(recombean.get(position).reach_money);
                holder.Commission.setText(recombean.get(position).proportion);
            } else {
                holder.name.setText(recombean1.get(position).name);
                holder.grade.setText(recombean1.get(position).level);
                holder.carry.setText(recombean1.get(position).reach_money);
                holder.Commission.setText(recombean1.get(position).proportion);
            }


        }

        @Override
        public int getItemCount() {
            if (flag) {
                return recombean.size();
            } else {
                return recombean1.size();
            }
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            TextView name, grade, carry, Commission;
            LinearLayout itemlayout;

            public MViewHolder(@NonNull View itemView) {
                super(itemView);
                itemlayout = itemView.findViewById(R.id.itemlayout);
                name = itemView.findViewById(R.id.name);
                grade = itemView.findViewById(R.id.grade);
                carry = itemView.findViewById(R.id.carry);
                Commission = itemView.findViewById(R.id.Commission);
            }
        }
    }
}
